{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Auto Review Commit Test Notebook\n",
    "\n",
    "This notebook tests the code review functionality of AutoReviewCommit class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autocoder.agent.auto_review_commit import AutoReviewCommit\n",
    "from byzerllm import SimpleByzerLLM\n",
    "import os\n",
    "from rich.console import Console\n",
    "from rich.panel import Panel\n",
    "from autocoder.common import AutoCoderArgs\n",
    "# Initialize LLM\n",
    "api_key = os.getenv(\"MODEL_DOUBAO_TOKEN\")\n",
    "\n",
    "llm = SimpleByzerLLM()\n",
    "llm.deploy(\n",
    "    model_path=\"\",\n",
    "    pretrained_model_type=\"saas/openai\",\n",
    "    udf_name=\"deepseek_chat\",\n",
    "    infer_params={\n",
    "        \"saas.base_url\": \"https://ark.cn-beijing.volces.com/api/v3\",\n",
    "        \"saas.api_key\": api_key,\n",
    "        \"saas.model\": \"ep-20250204215011-vzbsg\",\n",
    "        \"saas.is_reasoning\": False\n",
    "    }\n",
    ")\n",
    "\n",
    "# Initialize AutoReviewCommit\n",
    "project_dir = \"/Users/allwefantasy/projects/auto-coder\"\n",
    "args = AutoCoderArgs(\n",
    "    source_dir=project_dir\n",
    ")\n",
    "reviewer = AutoReviewCommit(llm=llm, args=args)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Latest commit information:\n",
      "\n",
      "Query: 将 @src/autocoder/agent/auto_review_commit.py 中\n",
      "的 return self.review.with_llm(self.llm).run(commits) 替换成 @src/autocoder/utils/auto_coder_utils/chat_stream_out.py 中的 stream_out 方法来实现\n",
      "\n",
      "Modified files:\n",
      "- /Users/allwefantasy/projects/auto-coder/src/autocoder/common/printer.py\n",
      "- /Users/allwefantasy/projects/auto-coder/src/autocoder/agent/auto_review_commit.py\n",
      "- /Users/allwefantasy/projects/auto-coder/src/autocoder/utils/auto_coder_utils/chat_stream_out.py\n",
      "- /Users/allwefantasy/.auto-coder/memory/.tmp/memory/1737527055.txt\n",
      "- /Users/allwefantasy/.auto-coder/memory/.tmp/memory/1737529595.txt\n",
      "- /Users/allwefantasy/.auto-coder/memory/.tmp/memory/1737537901.txt\n",
      "- /Users/allwefantasy/.auto-coder/memory/.tmp/memory/1738399572.txt\n",
      "- /Users/allwefantasy/.auto-coder/memory/.tmp/memory/1738483974.txt\n",
      "\n",
      "Diff preview:\n",
      "diff --git a/src/autocoder/agent/auto_review_commit.py b/src/autocoder/agent/auto_review_commit.py\n",
      "index 992d23e5..fbcfb1de 100644\n",
      "--- a/src/autocoder/agent/auto_review_commit.py\n",
      "+++ b/src/autocoder/agent/auto_review_commit.py\n",
      "@@ -26,7 +26,8 @@ class AutoReviewCommit:\n",
      "     def __init__(self, llm: byzerllm.ByzerLLM,\n",
      "                  project_dir: str,\n",
      "                  skip_diff: bool = False,\n",
      "-                 file_size_limit: int = 100):\n",
      "+                 file_size_limit: int = 100,\n",
      "+           ...\n"
     ]
    }
   ],
   "source": [
    "# Test parsing history tasks\n",
    "history_tasks = reviewer.parse_history_tasks()\n",
    "print(\"Latest commit information:\")\n",
    "for query, urls, diff in history_tasks:\n",
    "    print(\"\\nQuery:\", query)\n",
    "    print(\"\\nModified files:\")\n",
    "    for url in urls:\n",
    "        print(f\"- {url}\")\n",
    "    print(\"\\nDiff preview:\")\n",
    "    print(diff[:500], \"...\" if len(diff) > 500 else \"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a93c609347df4e279e9046ac449e8277",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Output()"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'```markdown\\n# 代码审查报告\\n\\n## 1. Issues\\n\\n### 1.1 代码可读性\\n- **问题**: `stream_out` 方法的导入语句放在了方法内部，这会影响代码的可读性和维护性。\\n- **严重程度**: low\\n\\n### 1.2 代码风格\\n- **问题**: `console` 参数在 `__init__` 方法中被添加，但没有在类中显式地声明为成员变量。\\n- **严重程度**: medium\\n\\n### 1.3 实现逻辑\\n- **问题**: `stream_out` 方法的返回值是 `content`，但原代码中 `run` 方法的返回值可能包含更多的信息（如 `meta`），直接返回 `content` 可能会导致信息丢失。\\n- **严重程度**: medium\\n\\n### 1.4 异常处理\\n- **问题**: 在 `try` 块中捕获了异常，但异常处理逻辑过于简单，仅打印了错误信息，没有进行进一步的错误处理或日志记录。\\n- **严重程度**: medium\\n\\n### 1.5 资源管理\\n- **问题**: `stream_out` 方法可能会涉及到资源（如网络连接、文件句柄等）的使用，但代码中没有显式地处理这些资源的释放。\\n- **严重程度**: medium\\n\\n## 2. Suggestions\\n\\n### 2.1 代码可读性\\n- **建议**: 将 `stream_out` 方法的导入语句移到文件顶部，与其他导入语句放在一起。\\n\\n### 2.2 代码风格\\n- **建议**: 在 `__init__` 方法中显式地将 `console` 参数赋值给类的成员变量，例如 `self.console = console`。\\n\\n### 2.3 实现逻辑\\n- **建议**: 确保 `stream_out` 方法的返回值与原 `run` 方法的返回值一致，或者明确说明返回值的变化，并在调用方进行相应的处理。\\n\\n### 2.4 异常处理\\n- **建议**: 在捕获异常后，除了打印错误信息外，还应考虑将错误信息记录到日志中，并根据具体情况决定是否需要重新抛出异常或进行其他处理。\\n\\n### 2.5 资源管理\\n- **建议**: 确保 `stream_out` 方法中使用的资源在使用完毕后被正确释放。如果 `stream_out` 方法内部已经处理了资源释放，则无需额外处理。\\n\\n## 3. Severity\\n- **low**: 1.1\\n- **medium**: 1.2, 1.3, 1.4, 1.5\\n\\n## 4. Affected Files\\n- `/Users/allwefantasy/projects/auto-coder/src/autocoder/agent/auto_review_commit.py`\\n\\n## 5. Summary\\n总体来看，代码变更实现了任务需求，但在代码可读性、风格、实现逻辑、异常处理和资源管理等方面存在一些改进空间。建议按照上述建议进行修改，以提高代码的质量和可维护性。\\n```'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reviewer.review_commit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "byzerllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
